package com.example.lte.second.api;

import com.example.lte.config.LinuxEnvironmentConfig;
import com.example.lte.config.WindowsEnvironmentConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Properties;

/**
 * @author lzh
 * @date 2020/11/17 - 13:46
 */
@RestController
@RequestMapping("/sec")
@Api(tags = "业务A模块接口")
public class SecondTestApi {

    private WindowsEnvironmentConfig windowsEnvironmentConfig;

    @Autowired
    public void setWindowsEnvironmentConfig(WindowsEnvironmentConfig windowsEnvironmentConfig) {
        this.windowsEnvironmentConfig = windowsEnvironmentConfig;
    }

    @Autowired
    public void setLinuxEnvironmentConfig(LinuxEnvironmentConfig linuxEnvironmentConfig) {
        this.linuxEnvironmentConfig = linuxEnvironmentConfig;
    }

    private LinuxEnvironmentConfig linuxEnvironmentConfig;

    @GetMapping("/getTsVideo")
    public OutputStream getVideoSrc(HttpServletResponse httpServletResponse) {


        FileSystemResource resource;
        Properties props = System.getProperties();
        String os = props.getProperty("os.name");
        if (os.startsWith("Win")) {
            resource = new FileSystemResource(windowsEnvironmentConfig.getFileUploadDir() + "demo1122.ts");
        } else {
            resource = new FileSystemResource(linuxEnvironmentConfig.getFileUploadDir() + "demo1122.ts");
        }


        //1.创建文件对象

        File f = resource.getFile();


        //2.获取文件名称
        String fileName = f.getName();
        //3.导出文件
        InputStream fis = null;
        OutputStream outputStream = null;

        try {
            //4.获取输入流
            fis = new BufferedInputStream(new FileInputStream(f.getPath()));
            byte[] buffer;
            buffer = new byte[fis.available()];
            fis.read(buffer);
            httpServletResponse.reset();
            //6.设置response编码
            httpServletResponse.setCharacterEncoding("UTF-8");

            httpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            httpServletResponse.addHeader("Content-Length", "" + f.length());
            //设置输出文件类型
            httpServletResponse.setContentType("video/mp2t");
            //7.获取response输出流
            outputStream = httpServletResponse.getOutputStream();
            outputStream.flush();
            //8.输出文件
            outputStream.write(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if (fis != null) {
                    fis.close();
                }

                if (outputStream != null) {
                    outputStream.flush();
                }

                if (outputStream != null) {
                    outputStream.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return outputStream;
    }


    @GetMapping("/t1")
    @ApiOperation(value = "测试t1", notes = "SecondTestApi测试")
    public String te() {
        return "SecondTestApi测试!";
    }
}
